Skip to content

feat(chat): add always-on OpenAI and Moshi voice conversations - #54

Merged
TudorAndrei merged 12 commits into
mainfrom
feat/start-example-model
Nov 6, 2026
Merged

TudorAndrei merged 12 commits into
mainfrom
feat/start-example-model

Conversation

@TudorAndrei

@TudorAndrei TudorAndrei commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

The chat example required a recorded utterance before it could produce a reply. This change adds persistent OpenAI Realtime and local Moshi connections through AudioFlow.Duplex. Select Start conversation once: microphone input continues during silence and reply playback until Stop.

OpenAI uses provider speech detection, keeps conversation context on one connection, and stops queued playback when the user speaks. Playback sample counts limit history truncation to the rendered prefix. The session bounds input, responses, time and owner queues, and closes when its owner exits. Existing committed-turn adapters remain available.

The chat page now shows provider and voice choices, call controls, status and conversation. Setup documentation and unrelated transport links are removed from the conversation view. Diagnostics stay collapsed. Build paths separate native and demo profiles.

Validation:

  • Core suite: 224 passed; targeted media, Moshi and provider tests: 38 passed.
  • Chat suite: 26 passed; browser audio JavaScript tests: 17 passed; asset build passed.
  • Live OpenAI check: two spoken utterances and replies on one connection, continuous silence accepted, and a word from the first message recalled in the second reply.
  • Live Moshi continuous-input check and running LiveView-to-OpenAI connection check passed.

The browser requests echo cancellation, but acoustic input/output has not been independently measured. Playback acknowledgement uses the browser audio clock. Moshi model context persists until its model service restarts.


Autofix with [code]smith
Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.

AUDIOFLOW_MEDIA=1 adds the Membrane dependencies that AudioFlow.Duplex.Moshi needs. Each profile now compiles to its own build path, so a profile change does not reuse stale modules.
Socket.open/4 connects and completes the upgrade before a deadline and
returns the connection to the caller, so a long-lived client can reuse the
handshake. Socket.endpoint!/1 validates ws/wss endpoints.
The Local Moshi full duplex pipeline uses AudioFlow.Duplex. The browser captures the microphone continuously, resamples it to 24 kHz and sends 80 ms chunks with acknowledgement-based flow control, while model audio plays at the same time. CHAT_MODE selects the startup mode; AUDIOFLOW_MOSHI_ENDPOINT is the only endpoint source.
AudioFlow.Moshi.Codec holds the Moshi wire kinds and the Ogg page and Opus
packet coding, so a continuous client can reuse them. The local Moshi
fixture moves to test/support with manual handshake and reply controls.
AudioFlow.Duplex keeps one model connection open. Microphone input and
model output flow at the same time, with no committed turn, silence gate or
output drop. It starts asynchronously, emits :ready after the Moshi
handshake, monitors the owner, and bounds unanswered input, push size,
owner mailbox, connect, idle and total time.

AudioFlow.Duplex.Moshi sends each 80 ms frame as one Ogg Opus page.
AudioFlow.Duplex.OpenAIRealtime keeps one server-owned OpenAI Realtime GA
WebSocket open per call. It configures server VAD with create_response and
interrupt_response, emits :ready only after session.updated, appends input
without commits or response.create, and keeps the provider conversation
for the whole connection.

Duplex core additions:
- adapter input_flow :realtime bounds input against the wall clock, so
  continuous silence never fills the input lead (Moshi keeps :answered)
- adapter headers can be a function, resolved only to connect
- events {:speech_started, item}, {:transcript, item, text | :failed},
  {:response, id, status} and {:output, item}
- Duplex.control/2 with optional handle_control/2; OpenAI uses
  {:played, item, ms} to send conversation.item.truncate, capped to the
  received audio, once, for the last interrupted reply only
The OpenAI mode ("OpenAI live conversation") now uses AudioFlow.Duplex
with AudioFlow.Duplex.OpenAIRealtime: Start once, the microphone stays
open and streams until Stop, and the model replies by voice. On user
speech the browser flushes queued reply audio without releasing capture,
drops late audio of the cut reply, and reports heard time so the server
truncates the provider history. Utterances show as transcript messages,
replies as assistant messages. OPENAI_REALTIME_ENDPOINT optionally
replaces the endpoint from the server environment.

The main view for all modes now has the title, the provider and voice
selectors, the conversation controls, a short status and the
conversation. Standalone transport links, prerequisite lists, timing
notes and disclaimers move to the README. Transcript saving and
playback diagnostics go to collapsed Settings and Diagnostics sections.
@TudorAndrei
TudorAndrei merged commit 2b78cad into main Nov 6, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant